<?php
namespace App\Admin\Controllers\Traits\WxUsedTraits;

use App\Admin\Renderable\BannerRender;
use App\Admin\Renderable\IconRender;
use App\Jobs\System\SettingImgToJpgJob;
use App\Models\WxIcon;
use App\Models\WxSlideshow;
use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Widgets\Tooltip;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

trait formUsedTrait{

    public function index_config(Content $content)
    {
        return $content->title('二手系统配置')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_config()->render()}</div>
HTML
            );
    }
    protected function form_config(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();

        $form->tab('基本', function (Form $form) {

            $res30 = __system__paycode__tip__(30);
            Tooltip::make('.mini_global_admin_open_not_support_tip')
                ->bottom()
                ->title($res30['tip']);

            $form->html(function () use ($res30){
                return '<div>闲置系统 '.$res30['text'].'</div>';
            }, '');

            $form->html(function () use ($res30){
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">一、订单分成</div>';
            }, '');
            $form->rate('app_used_order_division', '闲置订单平台分成')->default(Settings::get('app_used_order_division', 1.0));


            $form->divider();

            $form->html(function () use ($res30){
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">二、审核相关</div>';
            }, '');

            $form->radio('is_audit_used_good', '商品是否需要审核')->help('AI审核需要配置百度文字审核API，<a href="'.admin_url('settings/api').'" target="_blank">点击进入</a>')->options($this->audit_options)->default(Settings::get('is_audit_used_good', 0))->when('!=', 1, function (Form $form){
                $form->checkbox('audit_used_good_white_list', '闲置商品审核白名单')->options(FieldUtils::userPermissions())->default(Settings::get('audit_used_good_white_list', ''));
            });

            $form->divider();

            $form->radio('is_audit_used_comment', '留言是否需要审核')->help('AI审核需要配置百度文字审核API，<a href="'.admin_url('settings/api').'" target="_blank">点击进入</a>')->options($this->audit_options)->default(Settings::get('is_audit_used_comment', 0))->when('!=', 1, function (Form $form){
                $form->checkbox('audit_used_comment_white_list', '留言审核白名单')->options(FieldUtils::userPermissions())->default(Settings::get('audit_used_comment_white_list', ''));
            });
            $form->divider();
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">三、首页左右侧，轮播</div>';
            }, '');

            $form->multipleSelectTable('app_used_header_icon', '头部左右侧')
                ->title('图标库')
                ->max(2)
                ->from(IconRender::make())
                ->model(WxIcon::class, 'id', 'name', 'selectTable:formUsedTrait:app_used_header_icon')
                ->help('添加两个，若把闲置设置为tabbar，则可以显示， <a href="'.admin_url('settings/icons').'" target="_blank">管理图标库>></a><a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">（ps：如何控制顺序?）&nbsp;</a>')
                ->default(Settings::get('app_used_header_icon', '', true));

            $form->multipleSelectTable('used_mall_index_banner', '轮播图')
                ->title('轮播图库')
                ->max(10)
                ->from(BannerRender::make())
                ->model(WxSlideshow::class, 'id', 'id', 'selectTable:formUsedTrait:used_mall_index_banner')
                ->help('<a href="'.admin_url('slideshow').'" target="_blank">运营管理-轮播图中心</a><a href="https://doc.minisns.cn/doc/70/" target="_blank" style="color: red;">（ps：如何控制顺序?）&nbsp;</a>')->default(Settings::get('used_mall_index_banner', ''));

            $form->divider();
            $form->html(function () {
                return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">四、分享</div>';
            }, '');
            $form->text('used_mall_index_share_title', '首页分享标题')->default(Settings::get('used_mall_index_share_title', '超超超强大的闲置系统'));
            $form->image('used_mall_index_share_image', '首页分享海报')->help('<a href="https://img.mini.minisns.cn/images/bg/ershou.png" download="members_popup_poster.jpg" target="_blank" style="color: steelblue">点击下载默认图</a>')->url('files/uploads')->uniqueName()->autoUpload()->default(Settings::get('used_mall_index_share_image', 'https://img.mini.minisns.cn/images/bg/ershou.png'));
            $form->text('used_mall_detail_share_title', '闲置商品分享标题')->help('[good_title]将被替换成商品标题')->default(Settings::get('used_mall_detail_share_title', '[good_title] - 闲置'));
        });

        $form->tab('发布', function (Form $form) {
            $form->radio('used_mall_push_force_bind_phone', '强制绑定手机号')->options([0=>'不强制', 1=>'强制'])->default(Settings::get('used_mall_push_force_bind_phone', 0, true));
            $form->radio('used_mall_push_force_real_name', '强制实名')->options([0=>'不强制', 1=>'强制'])->default(Settings::get('used_mall_push_force_real_name', 0, true));
            $form->divider();
            $form->radio('used_mall_push_force_classify', '强制选择分类')->options([0=>'不强制', 1=>'强制'])->default(Settings::get('used_mall_push_force_classify', 0, true));
            global $__MINI_GLOBAL_TENANT_ID__;
            $form->radio('used_mall_push_force_subscribe_mp', '强制关注公众号')->options([0=>'不强制', 1=>'强制'])->help($__MINI_GLOBAL_TENANT_ID__ > 0 ? '必须开启服务器地址' : '必须开启服务器地址，具体请去[全局配置-支付-微信-公众号]')->default(Settings::get('used_mall_push_force_subscribe_mp', 0, true));
        });


        $form->tab('详情', function (Form $form) {
            $form->switch('used_mall_detail_self_pick_show_map', '自提是否显示地图')->default(Settings::get('used_mall_detail_self_pick_show_map', 1, true));
            $form->switch('used_mall_detail_can_buy', '显示购买按钮')->help('多商户商城，闲鱼等类型需要 <span style="color: red;">EDI经营许可证</span>，你若没有，可以关闭该按钮')->default(Settings::get('used_mall_detail_can_buy', 1, true));
        });

        $form->action('used/action/config');
        return $form;
    }

    public function config(Request $request){
        SiteUtils::forget_config_data();

        $audit_used_good_white_list = $request->input('audit_used_good_white_list');
        $audit_used_good_white_list_ = [];
        if($audit_used_good_white_list){
            foreach ($audit_used_good_white_list as $i){
                if($i === null){
                    continue;
                }
                $audit_used_good_white_list_[] = (int)$i;
            }
        }

        Settings::set('audit_used_good_white_list', $audit_used_good_white_list_, true);


        $audit_used_comment_white_list = $request->input('audit_used_comment_white_list');
        $audit_used_comment_white_list_ = [];
        if($audit_used_comment_white_list){
            foreach ($audit_used_comment_white_list as $i){
                if($i === null){
                    continue;
                }
                $audit_used_comment_white_list_[] = (int)$i;
            }
        }

        Settings::set('audit_used_comment_white_list', $audit_used_comment_white_list_, true);

        Settings::set('app_used_header_icon', AdminUtils::_multipleSelectTable_order_process( 'selectTable:formUsedTrait:app_used_header_icon', $request->input('app_used_header_icon') ), true, true);
        Settings::set('used_mall_index_banner', AdminUtils::_multipleSelectTable_order_process('selectTable:formUsedTrait:used_mall_index_banner', $request->input('used_mall_index_banner')) , true);
        Settings::set('is_audit_used_good', $request->input('is_audit_used_good'), true);
        Settings::set('is_audit_used_comment', $request->input('is_audit_used_comment'), true);

        Settings::set('used_mall_detail_share_title', $request->input('used_mall_detail_share_title'), true);
        Settings::set('used_mall_index_share_title', $request->input('used_mall_index_share_title'), true);
        Settings::set('used_mall_index_share_image', $request->input('used_mall_index_share_image'), true);

        Settings::set('used_mall_push_force_bind_phone', $request->input('used_mall_push_force_bind_phone'), true, true);
        Settings::set('used_mall_push_force_real_name', $request->input('used_mall_push_force_real_name'), true, true);
        Settings::set('used_mall_push_force_classify', $request->input('used_mall_push_force_classify'), true, true);
        Settings::set('used_mall_push_force_subscribe_mp', $request->input('used_mall_push_force_subscribe_mp'), true, true);


        Settings::set('app_used_order_division', _between_($request->input('app_used_order_division'), 0, 100), true);
        Settings::set('used_mall_detail_self_pick_show_map', $request->input('used_mall_detail_self_pick_show_map'), true, true);
        Settings::set('used_mall_detail_can_buy', $request->input('used_mall_detail_can_buy'), true, true);


        SettingImgToJpgJob::dispatch(['used_mall_index_share_image']);

        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);
        return $this->response()->success('成功！');
    }

}
